<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="../../css//index.css" />
  <title>记事本</title>
  <style>
    .active {
      background-color: #e0e0e0;
    }
  </style>
</head>

<body>

  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <header class="header">
      <h1>小黑记事本</h1>
      <input placeholder="请输入任务" class="new-todo" v-model.trim="todoName" />
      <button class="add" @click="add">添加任务</button>
    </header>
    <!-- 列表区域 -->
    <section class="main">
      <ul class="todo-list">
        <li class="todo" v-for="(todo,index) in todoList" :key="todo.id" @mouseenter="changeCurrentIndex(index)"
          :class="currentIndex===index?'active':''">
          <div class="view">
            <span class="index">{{index+1}}</span>
            <label>{{todo.name}}</label>
            <button class="destroy" @click="remove(todo.id)"></button>
          </div>
        </li>
      </ul>
    </section>
    <!-- 统计和清空 -->
    <footer class="footer" v-if="todoList.length">
      <!-- 统计 -->
      <span class="todo-count">合 计:<strong> {{total}}</strong></span>
      <!-- 清空 -->
      <button class="clear-completed" @click="clearAll">清空任务</button>
    </footer>
  </section>


  <script src="../../javascript/vue.js"></script>
  <script>

    const app = new Vue({
      el: '#app',
      data: {
        currentIndex: 0,//当前的索引
        todoName: "",//输入框的值
        todoList: JSON.parse(localStorage.getItem("todoList")) || []// todoList任务数组
      },
      methods: {
        /* 添加 */
        add() {
          let { todoName } = this
          if (todoName === "") {
            return alert("输入不能为空")
          }
          const isAddFlag = this.todoList.some((item) => { return item.name === todoName })
          if (isAddFlag) {
            return alert("您输入的任务名称已经存在于列表")
          }
          this.todoList.unshift({ id: Date.now(), name: todoName })
          this.todoName = ""

        },
        /* 删除 */
        remove(id) {
          const flag = confirm("您确定要删除该任务吗？？")
          if (flag) {
            /* 使用findIndex根据id去数组里面查找元素，如果查到了则返回该元素的索引 */
            const index = this.todoList.findIndex(item => item.id === id)
            /* 根据对应的索引去删除元素 */
            this.todoList.splice(index, 1)
          }
        },
        /* 清空 */
        clearAll() {
          const flag = confirm("您确定要清空所有任务吗？？")
          if (flag) {
            this.todoList = []
          }
        },
        changeCurrentIndex(index) {
          this.currentIndex = index
        }
      },
      computed: {
        total() {
          return this.todoList.length
        }
      },
      watch: {
        todoList: {
          deep: true,
          handler(newTodoList, oldTodoList) {
            localStorage.setItem("todoList", JSON.stringify(newTodoList))
          }
        }
      }
    })

  </script>
</body>

</html>